﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web.Script.Serialization;
using TestWSTMS.WSVDC3;
using VDC.VONE.ENTITIES;
using System.IO;

namespace TestWSTMS {
    public partial class Form1 : Form {
        public static WSVDC3.ServiceVOne wsvone {
            get;
            set;
        }
        public Form1() {
            InitializeComponent();
            GetTest();
            
            wsvone = new ServiceVOne();
            WSVDC3.AuthenticationHeader objAuthSoapHeader = new WSVDC3.AuthenticationHeader();
            objAuthSoapHeader.UserName = "service";
            objAuthSoapHeader.Password = "123456a@";
            wsvone.AuthenticationHeaderValue = objAuthSoapHeader;

            KhoiTao();
        }
        public void KhoiTao() {
            string json = wsvone.GetProfileTypeGroupsAll();
            GetProfileTypeGroupsAllModel linhvumodel = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<GetProfileTypeGroupsAllModel>(json);

            if (linhvumodel.status == 1) {
                cmbLinhVu.DataSource = linhvumodel.model;
                cmbLinhVu.DisplayMember = "Name";
                cmbLinhVu.ValueMember = "ProfileTypeGroupId";
            }

            /*Câu 7: Lấy danh sách trạng thái cấp*/
            json = wsvone.GetLevelAll();
            GetLevelAllModel LevelModel = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<GetLevelAllModel>(json);


            if (LevelModel.status == 1) {
                cmbTrangThaiCap.DataSource = LevelModel.model;
                cmbTrangThaiCap.DisplayMember = "Name";
                cmbTrangThaiCap.ValueMember = "LevelId";
            }
            

        }

        public void GetTest() {
            wsvone = new ServiceVOne();
            WSVDC3.AuthenticationHeader objAuthSoapHeader = new WSVDC3.AuthenticationHeader();
            objAuthSoapHeader.UserName = "service";
            objAuthSoapHeader.Password = "123456a@";

            wsvone.AuthenticationHeaderValue = objAuthSoapHeader;

            //string json =  wsvone.GetProfileTypeGroupsAll();
            //GetProfileTypeGroupsAllModel customerExten = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<GetProfileTypeGroupsAllModel>(json);

            //string json = wsvone.GetProfileTypeByProfileTypeGroups(5);
            //GetProfileTypeByProfileTypeGroupsModel customerExten = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<GetProfileTypeByProfileTypeGroupsModel>(json);

            //string json = wsvone.GetProfileTypeById(69);
            //GetProfileTypeByIdModel customerExten = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<GetProfileTypeByIdModel>(json);

            //chua
            /*test [câu 8] tra cứu hồ sơ theo mã*/
            //string json = wsvone.GetProfileByCode("MH85");
            //GetProfileTypeByIdModel customerExten = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<GetProfileTypeByIdModel>(json);

            /*test [câu 7] Lấy danh sách trạng thái cấp*/
            //string json = wsvone.GetLevelAll();
            //GetLevelAllModel customerExten = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<GetLevelAllModel>(json);


            /*test [câu 6] Lấy thành phần hồ sơ theo ID thủ tục*/
            //string json = wsvone.GetCompositionProfileListByIdProfileType(15);
            //GetCompositionProfileListByIdProfileTypeModel customerExten = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<GetCompositionProfileListByIdProfileTypeModel>(json);


            /*test [câu 5] Lấy căn cứ pháp lý theo ID thủ tục*/
            //string json = wsvone.GetLegalGroundsListByIdProfileType(15);
            //GetLegalGroundsListByIdProfileTypeModel customerExten = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<GetLegalGroundsListByIdProfileTypeModel>(json);


            /*test [câu 4] Lấy căn cứ pháp lý theo ID thủ tục*/
            //string json = wsvone.GetRequirementsListByIdProfileType(304);
            //GetRequirementsListByIdProfileTypeModel customerExten = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<GetRequirementsListByIdProfileTypeModel>(json);


            /*test [câu 3] Lấy thủ tục theo ID thủ tục*/
            //string json = wsvone.GetProfileTypeById(15);
            //GetProfileTypeByIdModel customerExten = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<GetProfileTypeByIdModel>(json);


            /*test [câu 2] Lấy danh sách thủ tục theo lĩnh vực*/
            //string json = wsvone.GetProfileTypeByProfileTypeGroups(36);
            //GetProfileTypeByProfileTypeGroupsModel customerExten = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<GetProfileTypeByProfileTypeGroupsModel>(json);

            /*test [câu 1] Lấy danh sách lĩnh vực*/
            //string json = wsvone.GetProfileTypeGroupsAll();
            //GetProfileTypeGroupsAllModel customerExten = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<GetProfileTypeGroupsAllModel>(json);


            /*test [câu 9] Tiếp nhận hồ sơ*/
            //string json = wsvone.TiepNhanHoSo();
            //GetProfileTypeGroupsAllModel customerExten = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<GetProfileTypeGroupsAllModel>(json);





        }
        

        private void button3_Click(object sender, EventArgs e) {
            openFileDialog1.Title = "Open File";
            openFileDialog1.Filter = "All Files|*.*";
            openFileDialog1.FileName = "";

            try {
                openFileDialog1.InitialDirectory = "C:\\Temp";
            }
            catch {
                // skip it 
            }

            openFileDialog1.ShowDialog();

            if (openFileDialog1.FileName == "")
                return;
            else
                txtFileNameNoiDung.Text = openFileDialog1.FileName;
        }

        private void button4_Click(object sender, EventArgs e) {
            openFileDialog2.Title = "Open File";
            openFileDialog2.Filter = "All Files|*.*";
            openFileDialog2.FileName = "";

            try {
                openFileDialog2.InitialDirectory = "C:\\Temp";
            }
            catch {
                // skip it 
            }

            openFileDialog2.ShowDialog();

            if (openFileDialog2.FileName == "")
                return;
            else
                txtFileNameHosogoc.Text = openFileDialog2.FileName;
        }

        private void button2_Click(object sender, EventArgs e) {
            TiepNhanHoSoModel cus1 = new TiepNhanHoSoModel();

            JavaScriptSerializer jss = new JavaScriptSerializer();
            jss.MaxJsonLength = int.MaxValue;/*tăng kích thước cho json*/

            cus1.ProfileTypeId = ConvertType.ToInt(cmbThuTuc.SelectedValue);
           
                cus1.Level = new Level() {
                    LevelId = 1,
                    Name = "Cấp lần đầu"
                };


            string json = wsvone.GetCompositionProfileListByIdProfileType(ConvertType.ToInt(cmbThuTuc.SelectedValue));
            GetCompositionProfileListByIdProfileTypeModel thanhphanhoso = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<GetCompositionProfileListByIdProfileTypeModel>(json);
            List<CompositionProfileList> listcom = new List<CompositionProfileList>();
            foreach (var item in chkListThanhPhanHoSo.CheckedItems) {
                CompositionProfileList com= thanhphanhoso.model.Where(p => p.Name == item.ToString()).FirstOrDefault();
                listcom.Add(com);
            }
            cus1.Composition = listcom;


            cus1.Object =  false;
            cus1.Name = txtTenHoSo.Text;
            cus1.OrganName = txtTenToChuc.Text;
            cus1.Attachments = UploadFile(txtFileNameNoiDung.Text);
            cus1.AttachmentsOriginal = UploadFile(txtFileNameHosogoc.Text);


            string jsonobject = jss.Serialize(cus1);

            //Goi webservice
            wsvone = new ServiceVOne();
            WSVDC3.AuthenticationHeader objAuthSoapHeader = new WSVDC3.AuthenticationHeader();
            objAuthSoapHeader.UserName = "service";
            objAuthSoapHeader.Password = "123456a@";

            wsvone.AuthenticationHeaderValue = objAuthSoapHeader;


            string strResult = wsvone.TiepNhanHoSo(jsonobject);



        }

        /// <summary>
        /// Upload any file to the web service; this function may be
        /// used in any application where it is necessary to upload
        /// a file through a web service
        /// </summary>
        /// <param name="filename">Pass the file path to upload</param>
        private fileUpload UploadFile(string filename) {
            try {
                // get the exact file name from the path
                String strFile = System.IO.Path.GetFileName(filename);

                // create an instance fo the web service


                // ResulTiepNhanHoSoModel customerExten = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<ResulTiepNhanHoSoModel>(json);


                // get the file information form the selected file
                FileInfo fInfo = new FileInfo(filename);

                // get the length of the file to see if it is possible
                // to upload it (with the standard 4 MB limit)
                long numBytes = fInfo.Length;
                double dLen = Convert.ToDouble(fInfo.Length / 1000000);

                // Default limit of 4 MB on web server
                // have to change the web.config to if
                // you want to allow larger uploads
                if (dLen < 4) {
                    // set up a file stream and binary reader for the 
                    // selected file
                    FileStream fStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
                    BinaryReader br = new BinaryReader(fStream);

                    // convert the file to a byte array
                    byte[] data = br.ReadBytes((int)numBytes);
                    br.Close();

                    // pass the byte array (file) and file name to the web service
                    // string sTmp = srv.UploadFile(data, strFile);
                    fStream.Close();
                    fStream.Dispose();

                    // this will always say OK unless an error occurs,
                    // if an error occurs, the service returns the error message
                    //MessageBox.Show("File Upload Status: " + sTmp, "File Upload");

                    return new fileUpload() {
                        f = data,
                        fileName = strFile
                    };
                }
                else {
                    // Display message if the file was too large to upload
                    //MessageBox.Show("The fil selected exceeds the size limit for uploads.", "File Size");
                    return new fileUpload();
                }
            }
            catch (Exception ex) {
                // display an error message to the user
                // MessageBox.Show(ex.Message.ToString(), "Upload Error");

                return new fileUpload();
            }
        }

        private void Form1_Load(object sender, EventArgs e) {

        }

        private void cmbLinhVu_SelectedIndexChanged(object sender, EventArgs e) {
            cmbThuTuc.DataSource = null;
            chkListThanhPhanHoSo.Items.Clear();
            int ProfileTyeGroupId;
            bool IsNum = int.TryParse(cmbLinhVu.SelectedValue.ToString(), out ProfileTyeGroupId);
            if (IsNum) {
                string json = wsvone.GetProfileTypeByProfileTypeGroups(ProfileTyeGroupId);
                GetProfileTypeByProfileTypeGroupsModel thutucmodel = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<GetProfileTypeByProfileTypeGroupsModel>(json);


                if (thutucmodel.status == 1) {
                    cmbThuTuc.DataSource = thutucmodel.model;
                    cmbThuTuc.DisplayMember = "Code";
                    cmbThuTuc.ValueMember = "ProfileTypeId";
                }

            }
        }
        
        private void cmbThuTuc_SelectedIndexChanged(object sender, EventArgs e) {
            int ProfileTyeGroupId;
            bool IsNum = int.TryParse(cmbLinhVu.SelectedValue.ToString(), out ProfileTyeGroupId);
            if (IsNum && cmbThuTuc.SelectedValue!=null) {

                string json = wsvone.GetCompositionProfileListByIdProfileType(ConvertType.ToInt(cmbThuTuc.SelectedValue));
                GetCompositionProfileListByIdProfileTypeModel thanhphanhoso = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<GetCompositionProfileListByIdProfileTypeModel>(json);

                if (thanhphanhoso.status == 1) {
                    foreach (var item in thanhphanhoso.model) {
                        chkListThanhPhanHoSo.Items.Add(item.Name, false);
                    }
                }


            }
        }

        private void cmbThuTuc_ValueMemberChanged(object sender, EventArgs e) {
            int ProfileTyeGroupId;
            bool IsNum = int.TryParse(cmbLinhVu.SelectedValue.ToString(), out ProfileTyeGroupId);
            if (IsNum) {


                string json = wsvone.GetCompositionProfileListByIdProfileType(ConvertType.ToInt(cmbThuTuc.SelectedValue));
                GetCompositionProfileListByIdProfileTypeModel thanhphanhoso = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<GetCompositionProfileListByIdProfileTypeModel>(json);

                if (thanhphanhoso.status == 1) {
                    foreach (var item in thanhphanhoso.model) {
                        chkListThanhPhanHoSo.Items.Add(item.Name, false);
                    }
                }


            }
        }


        private void button1_Click(object sender, EventArgs e) {
            /*test [câu 8] tra cứu hồ sơ theo mã*/
            dataGridView1.DataSource = null;
            string json = wsvone.GetProfileByCode(textBox1.Text);
            GetProfileByCodeModel model = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<GetProfileByCodeModel>(json);

            if (model.status == 1) {
                dataGridView1.DataSource = model.model;
            }
        }
    }
}

public class ConvertType {
    public static int ToInt(object obj) {
        try {
            if (obj == null)
                return 0;
            int rs = System.Convert.ToInt32(obj);
            if (rs < 0)
                return 0;
            return rs;
        }
        catch {
            return 0;
        }
    }
}